<?php
$this->title = '老会员绑定';
$signPackage = \app\components\Jssdk::getSignPackage();
use yii\helpers\Html;
?>
    <link href="<?= \yii\helpers\Url::to('@web/base/css/bootstrap.min.css') ?>" rel="stylesheet">
    <link href="<?= \yii\helpers\Url::to('@web/base/css/css-loader.css?t=' . time()) ?>" rel="stylesheet">
    <link href="<?= \yii\helpers\Url::to('@web/momo/css/bootstrap-datepicker.min.css') ?>" rel="stylesheet">
    
        <div class="page-header text-center">
            <h1>老会员绑定</h1>
        </div>
        <div class="broad" style="max-width:800px;margin: 0 auto;">
            <div class="middlecontent" style="max-width:800px;margin: 0 auto;">
                <div class="content1">
                    <div class="formmain">
                        <?php $form = \yii\widgets\ActiveForm::begin(['action' => [""], 'method' => 'post', 'id' => 'customer-form', 'class' => 'survey_form']) ?>


                        <?= $form->field($model, 'mobile')->textInput(['maxlength' => true, 'placeholder' => '请输入手机号']) ?>


                        <input type="hidden" id="openid" name="openid" value="<?= $openid ?>">
                        <input type="hidden" id="appid" name="appid" value="<?= $appid ?>">
                        <input type="hidden" id="code" name="code" value="<?= $code ?>">

                        <div class="form-group">
                            <?= \yii\helpers\Html::button('绑 定', ['id' => 'submit-form', 'class' => 'btn btn-success']) ?>
                        </div>

                        <?php \yii\widgets\ActiveForm::end() ?>
                    </div>
                </div>
            </div>
        </div>
    
    
<script>
    var customerSaveUrl = "<?= \yii\helpers\Url::to(["customer-save"]); ?>"
</script>
<?=Html::jsFile('https://res.wx.qq.com/open/js/jweixin-1.2.0.js')?>

<?php
$js = '
    
    $("#submit-form").click(function(e){
        var openid = $("#openid").val();
        var mobile = $("#customer-mobile").val();
        var code = $("#code").val();
        var data = {
            mobile: mobile,
            openid: openid,
            wechat_code: code,
        };
        $.post(customerSaveUrl, data, function (re) {
            if (re.status == "error"){
                var mobileError = $("#customer-mobile").next();
                mobileError.text(re.msg);
                mobileError.css("color","#A94442");
                $("#customer-mobile").prev().css("color","#A94442");
                $("#customer-mobile").css("border-color","#A94442");
                $(".lds-modal").css("display", "none");
            }
            if (re.status == "success"){
                alert(re.msg);
                wx.closeWindow();
            }
        }, "json");
    })
';
$this->registerJs($js);
?>
<script>
    var signPackage = <?= json_encode($signPackage) ?>;
    console.log(signPackage);
    wx.config({
        debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
        appId: signPackage.appId, // 必填，公众号的唯一标识
        timestamp: signPackage.timestamp, // 必填，生成签名的时间戳
        nonceStr: signPackage.nonceStr, // 必填，生成签名的随机串
        signature: signPackage.signature,// 必填，签名，见附录1
        jsApiList: [
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'onMenuShareQZone',
        'startRecord',
        'stopRecord',
        'onVoiceRecordEnd',
        'playVoice',
        'pauseVoice',
        'stopVoice',
        'onVoicePlayEnd',
        'uploadVoice',
        'downloadVoice',
        'chooseImage',
        'previewImage',
        'uploadImage',
        'downloadImage',
        'translateVoice',
        'getNetworkType',
        'openLocation',
        'getLocation',
        'hideOptionMenu',
        'showOptionMenu',
        'hideMenuItems',
        'showMenuItems',
        'hideAllNonBaseMenuItem',
        'showAllNonBaseMenuItem',
        'closeWindow',
        'scanQRCode',
        'chooseWXPay',
        'openProductSpecificView',
        'addCard',
        'chooseCard',
        'openCard',
        ] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
    });

//    wx.ready(function(){
//        wx.closeWindow();
//        // config信息验证后会执行ready方法，所有接口调用都必须在config接口获得结果之后，config是一个客户端的异步操作，所以如果需要在页面加载时就调用相关接口，则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口，则可以直接调用，不需要放在ready函数中。
//    });

    wx.error(function(res){
        console.log(res);
        // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
    });

</script>